using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using Nop.Core;
using Nop.Data.Mapping.WindAlert;

namespace Nop.Data.WindAlert
{
    /// <summary>
    /// Object context
    /// </summary>
    public class WindAlertObjectContext : DbContext, IDbContext
    {
        #region Ctor

        public WindAlertObjectContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            //((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
        }
        
        #endregion

        #region Utilities

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new WindDataMap());
            modelBuilder.Configurations.Add(new WaveDataMap());
            modelBuilder.Configurations.Add(new ForecastDataMap());

            //disable EdmMetadata generation
            //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            base.OnModelCreating(modelBuilder);
        }

        
        #endregion

        #region Methods
        public string CreateDatabaseScript()
        {
            return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
        }

        public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
        {
            return base.Set<TEntity>();
        }

        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// </summary>
        /// <param name="sql">The command string</param>
        /// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        /// <returns>The result returned by the database after executing the command.</returns>
        public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
        {
            throw new NotImplementedException();
        }

		/// <summary>
		/// Detach an entity
		/// </summary>
		/// <param name="entity">Entity</param>
		public void Detach(object entity)
		{
			if (entity == null)
				throw new ArgumentNullException("entity");

			((IObjectContextAdapter)this).ObjectContext.Detach(entity);
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets a value indicating whether proxy creation setting is enabled (used in EF)
		/// </summary>
		public virtual bool ProxyCreationEnabled
		{
			get
			{
				return this.Configuration.ProxyCreationEnabled;
			}
			set
			{
				this.Configuration.ProxyCreationEnabled = value;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether auto detect changes setting is enabled (used in EF)
		/// </summary>
		public virtual bool AutoDetectChangesEnabled
		{
			get
			{
				return this.Configuration.AutoDetectChangesEnabled;
			}
			set
			{
				this.Configuration.AutoDetectChangesEnabled = value;
			}
		}

		#endregion

	}
}